package com.example.administrator.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.example.administrator.myapplication.BaseActivity;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.bean.PersonalDataBean;
import com.example.administrator.myapplication.utils.ClearEditText;
import com.example.administrator.myapplication.utils.Logger;
import com.example.administrator.myapplication.utils.SharedPreFerencesTools;
import com.example.administrator.myapplication.view.ToastView;
import com.loopj.android.http.RequestParams;
import com.sv.svbind.annotation.Bind;

@Bind(layout = R.layout.activity_login_n)
public class LoginNActivity extends BaseActivity {

    private ImageView logo;

    @Bind(id = R.id.account_log)
    private ClearEditText account;

    @Bind(id = R.id.password_log)
    private ClearEditText password;

    private Button commitBt;
    private CheckBox rememberCb;

    private PersonalDataBean dataBean;

    private String username;
    private String passwordStr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initJudge();
        initEvent();
    }

    private void initJudge() {
        if (SharedPreFerencesTools.getMemberName() == null) {
            logo.setImageResource(R.mipmap.welcome_user);
        } else if (SharedPreFerencesTools.getMemberName().equals("admin")) {
            logo.setImageResource(R.mipmap.angle);
        }
        Logger.i("ddd", "initJudge--->" + SharedPreFerencesTools.getTestStr() + "initJudge===>" + SharedPreFerencesTools.getMemberName());
        if (SharedPreFerencesTools.getTestStr().equals("yes")) {
            rememberCb.setChecked(true);
            if (SharedPreFerencesTools.getMemberName() != null) {
                rememberCb.setText("记住着呢");
                account.setText(SharedPreFerencesTools.getMemberName());
                password.setText(SharedPreFerencesTools.getMemberPw());
            }
        } else if (SharedPreFerencesTools.getTestStr().equals("no")) {
            rememberCb.setChecked(false);
            if (SharedPreFerencesTools.getMemberName() != null) {
                rememberCb.setText("记住用户");
            }
        }
    }

    private void initView() {
        logo = (ImageView) findViewById(R.id.logo_log);
//        account = (ClearEditText) findViewById(R.id.account_log);
//        password = (ClearEditText) findViewById(R.id.password_log);
        commitBt = (Button) findViewById(R.id.login_bt);
        rememberCb = (CheckBox) findViewById(R.id.remberCb);

    }

    private void initData() {
        if (TextUtils.isEmpty(account.getText().toString().trim())) {
            ToastView.ToastShow(LoginNActivity.this, "请输入用户名", Toast.LENGTH_SHORT);
            return;
        }
        if (TextUtils.isEmpty(password.getText().toString().trim())) {
            ToastView.ToastShow(LoginNActivity.this, "请输入密码", Toast.LENGTH_SHORT);
            return;
        }
    }

    private void initEvent() {
        password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (account.getText().toString().trim().equals("admin")) {
                    loadUserLogo(1);
                } else {
                    loadUserLogo(0);
                }
            }
        });
        commitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                judgeStr();
            }
        });
        rememberCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SharedPreFerencesTools.setTestStr("yes");
                    Log.i("ddd","ddd"+account.getText().toString().trim());
                    SharedPreFerencesTools.setMemberName(username);
                    SharedPreFerencesTools.setMemberPw(passwordStr);
                } else {
                    SharedPreFerencesTools.setTestStr("no");
                    SharedPreFerencesTools.setMemberName(null);
                    SharedPreFerencesTools.setMemberPw(null);
                }
            }
        });
    }

    private void judgeStr() {
        showLoadingDialog("登录中，请稍候···");
        RequestParams params = new RequestParams();
        username=account.getText().toString().trim();
        passwordStr=password.getText().toString().trim();
        SharedPreFerencesTools.setMemberName(username);
        SharedPreFerencesTools.setMemberPw(passwordStr);
        EMChatManager.getInstance().login(username, passwordStr, new EMCallBack() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                dismissLoadingDialog();
                EMGroupManager.getInstance().loadAllGroups();
                EMChatManager.getInstance().loadAllConversations();
                Log.i("main", "登陆聊天服务器成功！");
                startActivity(new Intent(LoginNActivity.this, ChatActivity.class));
            }

            @Override
            public void onProgress(int arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(int arg0, String arg1) {
                // TODO Auto-generated method stub
                dismissLoadingDialog();
                Log.i("main", "onError--->"+arg1);
            }
        });
       /* params.put("memberName", username);
        params.put("memberPassword",passwordStr );
        RequestUtils.ClientPost(NetConfig.getLoginUrl(), params, new NetCallBackTxt() {
            @Override
            public void onMySuccess(String result) {
                Log.i("ddd", "ddd" + result);
                dismissLoadingDialog();
                try {
                    JSONObject object = new JSONObject(result);
                            if (object.getString("code").equals("0")) {
                                dataBean=new Gson().fromJson(result,PersonalDataBean.class);

                                startActivity(new Intent(LoginNActivity.this, ChatActivity.class));
                                SharedPreFerencesTools.setUserId(dataBean.getData().getMember().getMemberId());
                                SharedPreFerencesTools.setUserIP(dataBean.getData().getMember().getLastLoginIp());
                                SharedPreFerencesTools.setUserPhone(dataBean.getData().getMember().getMemberTel());
//                                SharedPreFerencesTools.setUserPhone(dataBean.getData().getMember().getMemberTel());
//                                SharedPreFerencesTools.setUserPhone(dataBean.getData().getMember().getMemberTel());
//                                SharedPreFerencesTools.setUserPhone(dataBean.getData().getMember().getMemberTel());
//                                SharedPreFerencesTools.setUserPhone(dataBean.getData().getMember().getMemberTel());
                                finish();
                            } else {
                                ToastView.ToastShow(LoginNActivity.this,   object.getString("msg"), Toast.LENGTH_SHORT);
                            }
                    // }

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onMyFailure(Throwable result) {
                System.out.print(result);
            }

            @Override
            public void onMyStart() {
            }
        });*/
     /*   if (account.getText().toString().trim().equals("admin")) {
            if (password.getText().toString().trim().equals("admin")) {
                startActivity(new Intent(LoginNActivity.this, LActivity.class));
                super.finish();
            } else {
                ToastView.ToastShow(LoginNActivity.this, "密码错误", Toast.LENGTH_SHORT);
            }
        } else {
            ToastView.ToastShow(LoginNActivity.this, "用户名错误", Toast.LENGTH_SHORT);
        }*/
    }

    private void loadUserLogo(int i) {
        if (i == 0) {
            logo.setImageResource(R.mipmap.devil);
        } else {
            logo.setImageResource(R.mipmap.angle);
        }
    }
}
